package xcu.lxj.nettychat.common.utils;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.SneakyThrows;
import xcu.lxj.nettychat.user.domain.entity.User;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

/**
 * @Author XinJian Lei
 * @Date 2024/10/6 20:06
 * @Version 1.0
 */
public class MyLambdaUtils {

    @SneakyThrows
    public static <T> Class<?> getReturnType(SFunction<T, ?> function) {
        Method method = function.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(true);
        SerializedLambda serializedLambda = (SerializedLambda)method.invoke(function);
        String implClass = serializedLambda.getImplClass();
        return Class.forName(implClass.replace("/", ".")).getDeclaredMethod(serializedLambda.getImplMethodName()).getReturnType();
    }


    @SneakyThrows
    public static void main(String[] args) {

        System.out.println(MyLambdaUtils.getReturnType(User::getAvatar));

//        SFunction<User, ?> function = User::getAvatar;
//        Method method = function.getClass().getDeclaredMethod("writeReplace");
//        method.setAccessible(true);
//        SerializedLambda serializedLambda = (SerializedLambda)method.invoke(function);
//        String implClass = serializedLambda.getImplClass();
//        System.out.println(implClass);
//        System.out.println(Class.forName(implClass.replace("/", ".")).getDeclaredMethod(serializedLambda.getImplMethodName()).getReturnType());
//        String implMethodName = serializedLambda.getImplMethodName();
//        System.out.println(implMethodName);
//        System.out.println(serializedLambda.getInstantiatedMethodType());
//        System.out.println(serializedLambda.getCapturingClass());

    }

}
